//
//  HomeBasicViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 15-3-13.
//  Copyright (c) 2015年 RuiBen. All rights reserved.
//

#import "HomeBasicViewController.h"

@interface HomeBasicViewController ()
{
    NSUInteger previousSelectedIndex;
}
@end

@implementation HomeBasicViewController
@synthesize homeViewControllers = _homeViewControllers;

-(void)viewWillAppear:(BOOL)animated
{
    [Global setHomeCustomID:0];
    [Global setShopCartID:0];
    [Global setOrderID:0];
    [self setSelectedIndex:0];
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    if (selectedIndex<[self.homeViewControllers count]) {
        if (previousSelectedIndex != NSNotFound)
        {
            UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
            [previousViewController.view removeFromSuperview];
            [previousViewController removeFromParentViewController];
        }
        
        UIViewController *selectedViewController = [self.homeViewControllers objectAtIndex:selectedIndex];
        [self addChildViewController:selectedViewController];
        selectedViewController.view.frame = CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
        selectedViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        [self.view addSubview:selectedViewController.view];
        // remove previously selected view controller (if any)
        previousSelectedIndex=selectedIndex;
    }
    
}
-(void)customItemClick:(NSInteger)index
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:index];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=index;

}
-(void)signInBackBtnClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:0];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=0;
}
-(void)scanResult
{
    
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:9];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=9;
    
}
-(void)styleDetailBackClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:0];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=0;
}
-(void)buyNowClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:11];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=11;
}
-(void)cashierBackClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:9];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=9;
}
-(void)addToShopCartClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:10];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=10;
}
-(void)shopCartBackClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:9];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=9;
}
-(void)shopCartCashierBtnClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:11];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=11;
}
-(void)shopCartHomeBtnClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:12];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=12;

}
-(void)confirmDeliveryAddressBtnClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:10];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=10;

}
-(void)addNewMemberBtnClick
{
    UIViewController *selectedViewController=[self.homeViewControllers objectAtIndex:2];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.homeViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=2;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)setProductViewControllers:(NSArray *)viewControllers
{
    _homeViewControllers = [viewControllers copy];
    previousSelectedIndex=NSNotFound;
    // create tab bar items
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
